package br.com.sigad.servlets.documento;


import java.io.BufferedReader;
import java.io.IOException;
import java.text.DateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import br.com.sigad.persistencia.dao.documento.DocumentoDAO;
import br.com.sigad.persistencia.model.DocumentoEntity;
import br.com.sigad.persistencia.model.RepresentacaoDigitalEntity;
import br.com.sigad.persistencia.model.TipoDocumentoEntity;

/**
 * Servlet implementation class SalvarDocumento
 */
@WebServlet("/SalvarDocumento")
public class SalvarDocumento extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		try {
			DocumentoEntity documento = getParametros(request);
			
			DocumentoDAO dao = new DocumentoDAO();
			if(documento.getCodigo()!=null){
				dao.salvar(documento);//significa que é uma edicao
			}else{
				dao.cadastrar(documento);//significa que  é uma adicao
			}
			

			response.setContentType("text/plain");
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write("SUCESSO");
			return;
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}

		response.setContentType("text/plain");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write("algo estranho");
	}

	private DocumentoEntity getParametros(HttpServletRequest request)
			throws java.text.ParseException {

		StringBuffer sb = new StringBuffer();
		try {
			BufferedReader reader = request.getReader();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		JSONParser parser = new JSONParser();
		JSONObject joUser = null;
		try {
			joUser = (JSONObject) parser.parse(sb.toString());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		DocumentoEntity dto = new DocumentoEntity();
		if (joUser != null) {
			String codigo = (String)joUser.get("codigo");
			if(codigo!=null && !codigo.isEmpty()){
				dto.setCodigo(Long.parseLong(((String) joUser.get("codigo"))));
			}
			dto.setDescricao((String) joUser.get("descricao"));

			dto.setDataCriacao(DateFormat.getDateInstance().parse(
					(String) joUser.get("dataCriacao")));
			dto.setPeriodoPermanencia(Integer.parseInt((String) joUser
					.get("periodoPermanencia")));
			dto.setLocalArmazenamentoFisico((String) joUser
					.get("localArmazenamento"));
			
			RepresentacaoDigitalEntity representacaoDigital= new RepresentacaoDigitalEntity();
			representacaoDigital.setNome((String) joUser.get("nomeArquivo"));
			dto.setRepresentacaoDigital(representacaoDigital);
			
			TipoDocumentoEntity tipoDocumento = new TipoDocumentoEntity();
			tipoDocumento.setNome((String) joUser.get("tipo"));
			dto.setTipoDocumento(tipoDocumento);
		}

		return dto;

	}
}
